package com.laconic.vbenadmin.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author JiangLiu
 * @Date 2025/6/4 10:26
 **/
public class ExcelUtil {


    // 读取excel文件，导入到数据库中
    public static void main(String[] args) {
        readCityWeatherBaseExcel();
    }

    public static void readCityWeatherBaseExcel() {
        // 替换为你的 Excel 文件路径
        String fileName = "/Users/jiang/Downloads/AMap_adcode_citycode.xlsx";

        EasyExcel.read(fileName, CityWeatherBase.class, new ReadListener<CityWeatherBase>() {


            List<CityWeatherBase> cityWeatherBaseList = new ArrayList<>();
            int batchSize = 100;
            @Override
            public void invoke(CityWeatherBase cityWeatherBase, AnalysisContext context) {

                cityWeatherBaseList.add(cityWeatherBase);
                if (cityWeatherBaseList.size() >= batchSize) {
                    // insert TODO
                    cityWeatherBaseList.clear();
                }
                // 处理读取到的数据
                System.out.println("读取到的数据: " + cityWeatherBase.getCityName() + ", " + cityWeatherBase.getAdcode() + ", " + cityWeatherBase.getCitycode());

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                if (!cityWeatherBaseList.isEmpty()) {
                    // insert TODO
                }
            }
        }).sheet().doRead();
    }
}
